﻿@using AIStudio.Entity.Enum;
@inherits FeedbackBase

<Table @ref="_table"
       TItem="Base_LogSystemDTO"
       DataSource="Data"
       EnableVirtualization
       Loading="Loading"
       @bind-PageSize="Pagination.PageRows"
       ScrollX="800"
       ScrollBarWidth="12px"
       Size="TableSize.Small"
       RowClassName="@(x => x.RowIndex % 2 == 0 ?"gray-2":"")">
    <ChildContent>
        <AntDesign.Column Ellipsis Title="内容" DataIndex="Message" TData="string" />
        <AntDesign.Column Ellipsis Title="类别" DataIndex="LogType" TData="string" />
        <AntDesign.Column Ellipsis Title="名称" DataIndex="Name" TData="string" />
        <AntDesign.Column Ellipsis Title="操作人" DataIndex="CreatorName" TData="string" />
        <AntDesign.Column Ellipsis Title="时间" DataIndex="CreateTime" TData="DateTime" Format="yyyy-MM-dd HH:mm:ss" />
    </ChildContent>
    <PaginationTemplate>
        <div style="float:right;margin-top:10px">
            <Pagination Total="Pagination.Total"
                        ShowTotal="ShowTotal"
                        ShowSizeChanger
                        PageSize="Pagination.PageRows"
                        Current="Pagination.PageIndex"
                        OnChange="PageIndexChanged"
                        OnShowSizeChange="PageSizeChanged" />
        </div>
    </PaginationTemplate>
</Table>

<style>
    .gray-2 {
        background-color: #fafafa;
    }
</style>

@code {

    [Inject]
    protected IDataProvider DataProvider { get; set; }

    [CascadingParameter]
    public Error? Error { get; set; }

    bool Loading { get; set; }

    Func<PaginationTotalContext, string> ShowTotal { get; set; } = ctx => $"总数：{ctx.Total}   当前：{ctx.Range.from}-{ctx.Range.to}";

    Util.Common.Pagination Pagination { get; set; } = new Util.Common.Pagination() { PageRows = 20 };

    List<Base_LogSystemDTO> Data { get; set; }
    protected Table<Base_LogSystemDTO>? _table;
    [Parameter]
    public string FullName { get; set; }

    protected override async Task OnInitializedAsync()
    {
        using (var waitfor = WaitFor.GetWaitFor(this))
        {
            FullName = this.Options;
            await GetData();

            await base.OnInitializedAsync();
        }
    }

    private async Task GetData()
    {
        try
        {
            var data = new
            {
                PageIndex = Pagination.PageIndex,
                PageRows = Pagination.PageRows,
                SortField = Pagination.SortField,
                SortType = Pagination.SortType,
                SearchKeyValues = new Dictionary<string, object>()
                {
                    {"LogType", UserLogType.系统任务.ToString() },
                    {"Name", FullName}
                }
            };

            var result = await DataProvider.PostData<List<Base_LogSystemDTO>>($"/Base_Manage/Base_LogSystem/GetDataList", data.ToJson());
            if (!result.Success)
            {
                throw new MsgException(result.Msg);
            }
            Pagination.Total = result.Total;
            Data = new List<Base_LogSystemDTO>(result.Data);

        }
        catch (Exception ex)
        {
            await Error.ProcessError(ex);
        }
    }

    protected virtual async Task PageIndexChanged(PaginationEventArgs paginationEvent)
    {
        if (Pagination.PageIndex != paginationEvent.Page)
        {
            Pagination.PageIndex = paginationEvent.Page;
            await GetData();
        }
    }

    protected virtual async Task PageSizeChanged(PaginationEventArgs paginationEvent)
    {
        if (Pagination.PageRows != paginationEvent.PageSize)
        {
            Pagination.PageRows = paginationEvent.PageSize;
            await GetData();
        }
    }
}